/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.uc.manager.impl;

import com.xy.biz.uc.dao.TeamDao;
import com.xy.biz.uc.dao.TeamUserRelDao;
import com.xy.biz.uc.domain.bo.TeamBO;
import com.xy.biz.uc.domain.bo.TeamUserRelBO;
import com.xy.biz.uc.domain.converter.TeamConverter;
import com.xy.biz.uc.domain.converter.TeamUserRelConverter;
import com.xy.biz.uc.domain.po.TeamPO;
import com.xy.biz.uc.domain.po.TeamUserRelPO;
import com.xy.biz.uc.domain.query.TeamQuery;
import com.xy.biz.uc.domain.query.TeamUserRelQuery;
import com.xy.biz.uc.domain.req.DeleteTeamUserRelRequest;
import com.xy.biz.uc.domain.req.SaveTeamUserRelRequest;
import com.xy.biz.uc.domain.req.UpdateTeamUserRelRequest;
import com.xy.biz.uc.manager.TeamManager;
import com.xy.tool.context.UserContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *
 *
 * @author yzh
 * @date 2021-4-26
 */
@Service
public class TeamManagerImpl implements TeamManager {

    @Autowired
    private TeamDao teamDao;
    @Autowired
    private TeamUserRelDao teamUserRelDao;

	@Transactional
	@Override
	public int saveTeam(TeamBO teamBO) {
		TeamPO teamPO = TeamConverter.toPO(teamBO);
		int row = teamDao.saveTeam(teamPO);
		if (row > 0 && !CollectionUtils.isEmpty(teamBO.getTeamUserRelBOList())) {
			teamBO.getTeamUserRelBOList().forEach(e -> e.setTeamId(teamPO.getTeamId()));
			row = teamUserRelDao.saveAllTeamUserRel(TeamUserRelConverter.toPO(teamBO.getTeamUserRelBOList()));
		}
		return row;
	}

    @Override
    public int updateTeam(TeamBO teamBO) {
        return teamDao.updateTeam(TeamConverter.toPO(teamBO));
    }

    @Override
    public int deleteTeam(Long teamId) {
        return teamDao.deleteTeam(teamId);
    }

    @Override
    public TeamBO getTeam(Long teamId) {
        return TeamConverter.fromPO(teamDao.getTeam(teamId));
    }

    @Override
    public List<TeamBO> listTeamById(Collection<Long> collection) {
        return TeamConverter.fromPO(teamDao.listTeamById(collection));
    }

    @Override
    public List<TeamBO> listTeam(TeamQuery query) {
        return TeamConverter.fromPO(teamDao.listTeam(query));
    }

    @Override
    public int countTeam(TeamQuery query) {
        return teamDao.countTeam(query);
    }

    @Override
    public List<TeamUserRelBO> listTeamUserRel(TeamUserRelQuery query) {
        return TeamUserRelConverter.fromPO(teamUserRelDao.listTeamUserRel(query));
    }

    @Override
    public int deleteTeamUserRel(DeleteTeamUserRelRequest request) {
        int row = teamUserRelDao.deleteTeamUserRel(request);
        if (row > 0) {
            TeamPO team = teamDao.getTeam(request.getTeamId());
            team.setMemberSize(team.getMemberSize() - 1);
            teamDao.updateTeam(team);
        }
        return row;
    }

    @Override
    public int updateTeamUserRel(TeamUserRelBO teamUserRelBO) {
        return teamUserRelDao.updateTeamUserRel(TeamUserRelConverter.toPO(teamUserRelBO));
    }

    @Override
    public int inviteJoin(SaveTeamUserRelRequest request) {
        //未添加成员id
        List<Long> longs = teamUserRelDao.listTeamUserRelByUserIdList(TeamUserRelQuery.builder()
                .teamId(request.getTeamId())
                .userIdList(request.getUserIdList())
                .build());
        //如果有已加入成员移除
        if (!CollectionUtils.isEmpty(longs)) {
            request.getUserIdList().removeAll(longs);
        }
        //没有需要添加成员，直接返回
        if (CollectionUtils.isEmpty(request.getUserIdList())) {
            return 0;
        }

        //有则继续，添加团队信息
        TeamPO team = teamDao.getTeam(request.getTeamId());
        //判断当前添加人数是否超过上限
        if (team.getMemberSize() + request.getUserIdList().size() > team.getCapacitySize()) {
            return -1;
        }

        LocalDateTime now = LocalDateTime.now();
        List<TeamUserRelPO> teamUserRelPOS = new ArrayList<>();
        for (Long aLong : request.getUserIdList()) {
            TeamUserRelPO teamUserRelPO = new TeamUserRelPO();
            teamUserRelPO.setTeamId(request.getTeamId());
            teamUserRelPO.setJoinStatus(1);
            teamUserRelPO.setUserId(aLong);
            teamUserRelPO.setRole(0);
            teamUserRelPO.setCreateTime(now);
            teamUserRelPO.setJoinTime(now);
            teamUserRelPO.setCreateUserId(UserContextHolder.getUserId());
            teamUserRelPOS.add(teamUserRelPO);
        }
        int row = teamUserRelDao.inviteJoin(teamUserRelPOS);
        if (row > 0) {
            team.setMemberSize(team.getMemberSize() + row);
            teamDao.updateTeam(team);
        }
        return row;
    }

}
